// pages/login/login.js
const request = require("../../utils/request");
import { $await } from '../../utils/util';
import { loginAPI } from '../../api/public/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loginPhone:false,
    time: 60,
    sendMg: '获取验证码',
    sendNum: 0,
    isLogin: false,
    phone: '',
    codeColor:''
  },

  async wxLogin() {
    let _this = this
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        wx.login({
          success(res) {
            _this.login(res.code)
          }
        })
      }
    })
  },

  async login(code) {
    let [err, res] = await $await(loginAPI.oauth({
      code: code
    }))
    if (res) {
      console.log(res)
      let userInfo = res
      wx.setStorageSync('userInfo', userInfo)
      if (!userInfo.openid) {
        wx.showToast({
          title: '请重新进入小程序',
        })
        return ;
      }
      if (userInfo.openid && userInfo.mobile == '') {
        this.setData({
          loginPhone : true
        })
        return ;
      }
      if (this.data.isBack) {
        wx.reLaunch({
          url: '/pages/my/my',
        }) 
      } else {
        let url = wx.getStorageSync('Router')
        wx.redirectTo({
          url: '/'+url
        })
      }
    }
  },


  closePhone() {
    this.setData({
      phone: '',
      codeColor:''
    })

  },
  codes(e) {
    let code = e.detail.value;
    this.setData({
      code,
      isLogin: true
    })
  },
  phone(e) {
    let value = e.detail.value,
      that = this;
    this.setData({
      phone: value
    })
  },
  async getCode() {
    var that = this,
      reg = /^1\d{10}$/;
    if (this.data.time != 60) {
      return false;
    }
    if (!reg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号!',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    let [err, res] = await $await(loginAPI.sendCode({
      mobile: that.data.phone
    }))
    if (res) {
      that.sendCode()
    }

  },

  sendCode() {
    var that = this;
    var times = setInterval(function () {
      if (that.data.time == 0) {
        clearInterval(times);
        that.setData({
          sendMg: "获取验证码",
          sendNum: 0,
          time: 60
        })
        return false;
      } else {
        that.data.time--;
        // console.log(that.data.time)
        that.setData({
          time: that.data.time,
          sendMg: that.data.time + "后重新获取",
          sendNum: 1
        })
      }
    }, 1000)
  },


  getCodeValue() {
    if (this.data.phone && this.data.code) {
      this.setData({
        isLogin: true
      })
    }
    if(/^1\d{10}$/.test(this.data.phone)){
      this.setData({codeColor:'#FC404A'})
    }else{
      this.setData({codeColor:''})
    }
  },
  async loginPhones() {
    let that = this;
    let [err, res] = await $await(loginAPI.login({
      mobile: that.data.phone,
      code : this.data.code
    }))
    if (res) {
      console.log(res)
      wx.setStorageSync('userInfo', res)
      if (this.data.isBack) {
        wx.reLaunch({
          url: '/pages/my/my',
        }) 
      } else {
        let url = wx.getStorageSync('Router')
        wx.redirectTo({
          url: '/'+url
        })
      }
    }
  },
  getUrl() {  
    var pages = getCurrentPages() //获取加载的页面  
    var currentPage = pages[pages.length - 2] //获取上一个页面对象  
    var url = currentPage.route //当前页面url  
    wx.setStorageSync('Router', `${url}`)  
    var options = currentPage.options //如果要获取url中所带的参数可以查看options  
    //参数多时通过&拼接url的参数  
    if(options){
        var urlWithArgs = url + '?'  
        for (var key in options) {  
        var value = options[key]  
        urlWithArgs += key + '=' + value + '&'  
        }  
        urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1) 
        wx.setStorageSync('Router', `${urlWithArgs}`)  
    }
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getTime()
    console.log(options)
    if (options.isBack) {
      this.setData({
        isBack : true
      })
    }
    this.getUrl()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})